package LeetCode;

public class LC_169_MajorityElement {

    public static void main(String[] args) {

    }

    static class Solution {
        public int majorityElement(int[] num) {
            if (num.length < 3) return num[0];
            int majority = num[0];
            int count = 1;
            for (int i = 1; i < num.length; ++i) {
                if (count == 0) {
                    majority = num[i];
                    ++count;
                } else if (num[i] == majority)
                    ++count;
                else
                    --count;
            }
            return majority;
        }
    }

}